﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BuscadorDePreco.Data;

namespace BuscadorDePreco
{
    public partial class Segmentos : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
                AtualizaSegmentos();
        }

        protected void ExcluirBtn_Command(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "excluir")
            {
                if (e.CommandArgument != null && !string.IsNullOrEmpty(e.CommandArgument.ToString()))
                {
                    //ConfirmacaoExclusaoPopup.ItemId = e.CommandArgument.ToString();
                    //SegmentoIdHdf.Value = e.CommandArgument.ToString();
                    /*try
                    {
                        SegmentoTableAdapter segmentoTA = new SegmentoTableAdapter();
                        segmentoTA.ExcluirSeguimento(int.Parse(e.CommandArgument.ToString()));
                    }
                    catch (Exception ex)
                    {
                    }

                    AtualizaSegmentos();*/
                }
                //ModalPopupExtender_Process.Show();
                ConfirmacaoExclusaoPopup.ShowPopup();
            }
        }

        public void ExcluirSegmento(int segmentoId)
        {
            try
            {
                SegmentoTableAdapter segmentoTA = new SegmentoTableAdapter();
                segmentoTA.ExcluirSeguimento(segmentoId);

                ConfirmacaoExclusaoPopup.HidePopup();
                ConfirmacaoExclusaoPopup.ResetPopup();
                AtualizaSegmentos();
            }
            catch (Exception ex)
            {
                ConfirmacaoExclusaoPopup.ShowError();
            }

        }

        private void AtualizaSegmentos()
        {
            SegmentoTableAdapter segmentoTA = new SegmentoTableAdapter();
            Segmentos_ListView.DataSource = segmentoTA.RecuperarSegmentos();
            Segmentos_ListView.DataBind();
        }
    }
}